
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author leon
 */
public class TaskOMainScreen extends JFrame implements Runnable {

    //JFrame JFrame;
    UserInputParser userInputParser;

    public TaskOMainScreen() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
    }

    @Override
    public void run() {
        //JFrame = new JFrame();
        this.setVisible(true);
    }

    @Override
    public void inputFromUser(String inputString) {
        this.parseInput(inputString);
    }

    public void addToTable(List<Task> taskList) {
        this.writeTable(taskList);
    }

    private void parseInput(String inputString) {
        this.userInputParser = new UserInputParser();
        this.userInputParser.parseUserInput(inputString);
        List<Task> taskToDisplay = TaskO.taskList.getTempStorage();
        this.writeTable(taskToDisplay);
    }

    @Override
    public void pressSyncButton() {
        this.popLogin();
    }

    @Override
    public void pressLoginButton() {
        this.hideLogin();
    }

    @Override
    public void writeTable(List<Task> taskList) {
        TableModel taskModel = this.getListTable().getModel();
        DefaultTableModel taskDefModel = (DefaultTableModel) this.getListTable().getModel();
        taskDefModel.setRowCount(0);
        for (int i = 0; i < taskList.size(); i++) {
            if (i >= taskModel.getRowCount()) {
                this.addRowToModel();
            }
            Task curTask = taskList.get(i);
            String taskTitle = curTask.getTaskTitle();
            String taskDate = curTask.getTaskEndDate();
            String taskStatus = "";
            taskModel.setValueAt(i+1, i, 0);
            taskModel.setValueAt(taskTitle, i, 1);
            taskModel.setValueAt(taskDate, i, 2);
            taskModel.setValueAt(taskStatus, i, 3);
        }
    }
}
